#!/bin/bash

DIRECTORY="$(dirname "$(dirname "$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")")"

function error {
  echo -e "\\e[91m$1\\e[39m"
  exit 1
}

if [ -f "$HOME/.conkyrc" ]; then
  error "User error: Another Conky theme is already installed! Uninstall the original app (Or delete $HOME/.conkyrc), then try again."
fi

wget -O ~/.conkyrc https://raw.githubusercontent.com/Botspot/rpi_conky/master/conkyrc || error 'Failed to download conkyrc!!'

#change eth0 to wlan0 in conky config if user is currently using WiFi
interface="$(ip addr | awk '/state UP/ {print $2}' | tr -d ':' | head -n 1)"
if [ ! -z "$interface" ];then
  sed -i "s/eth0/$interface/g" ~/.conkyrc
fi


# Get dependencies
"${DIRECTORY}/pkg-install" "conky" "$(dirname "$0")" || exit 1

#run conky this time
conky &

#make autostart entry to run it next time
mkdir -p ~/.config/autostart
sudo rm -f ~/.config/autostart/conky.desktop
echo "[Desktop Entry]
Name=Conky
Type=Application
Exec=bash -c 'sleep 5;conky -q -d'
Terminal=false
Icon=${DIRECTORY}/apps/Conky/icon-64.png
Comment=system monitoring tool.
Categories=Utility;" > ~/.config/autostart/conky.desktop

